﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel.Design
Imports System.Globalization
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.Runtime

Namespace System.Workflow.Activities
    Friend Class CorrelationSetsValidator
        ' Methods
        Private Shared Function FetchParameterType(ByVal memberInfo As MemberInfo, ByVal paramPath As String) As Type
            Dim method As MethodInfo = Nothing
            If TypeOf memberInfo Is EventInfo Then
                method = Helpers.GetDelegateFromEvent(DirectCast(memberInfo, EventInfo)).GetMethod("Invoke")
            Else
                method = DirectCast(memberInfo, MethodInfo)
            End If
            Return CorrelationSetsValidator.GetCorrelationParameterType(paramPath, method.GetParameters)
        End Function

        Private Shared Sub FillCorrelationAliasAttrs(ByVal memberInfo As MemberInfo, ByVal correlationAliasAttrs As Hashtable, ByVal validationErrors As ValidationErrorCollection)
            Dim obj2 As Object
            For Each obj2 In memberInfo.GetCustomAttributes(GetType(CorrelationAliasAttribute), False)
                Dim attributeFromObject As CorrelationAliasAttribute = Helpers.GetAttributeFromObject([Of] CorrelationAliasAttribute)(obj2)
                If (String.IsNullOrEmpty(attributeFromObject.Name) OrElse (attributeFromObject.Name.Trim.Length = 0)) Then
                    Dim item As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationAttributeInvalid", New Object() { GetType(CorrelationAliasAttribute).Name, "Name", memberInfo.Name }), &H150)
                    item.UserData.Add(GetType(CorrelationAliasAttribute), memberInfo.Name)
                    validationErrors.Add(item)
                ElseIf (String.IsNullOrEmpty(attributeFromObject.Path) OrElse (attributeFromObject.Path.Trim.Length = 0)) Then
                    Dim error2 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationAttributeInvalid", New Object() { GetType(CorrelationAliasAttribute).Name, "Path", memberInfo.Name }), &H150)
                    error2.UserData.Add(GetType(CorrelationAliasAttribute), memberInfo.Name)
                    validationErrors.Add(error2)
                ElseIf correlationAliasAttrs.Contains(attributeFromObject.Name) Then
                    Dim error3 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_DuplicateCorrelationAttribute", New Object() { GetType(CorrelationAliasAttribute).Name, attributeFromObject.Name, memberInfo.Name }), &H151)
                    error3.UserData.Add(GetType(CorrelationAliasAttribute), memberInfo.Name)
                    validationErrors.Add(error3)
                Else
                    correlationAliasAttrs.Add(attributeFromObject.Name, attributeFromObject)
                End If
            Next
        End Sub

        Private Shared Function GetCorrelationParameterType(ByVal parameterPropertyName As String, ByVal parametersCollection As Object) As Type
            Dim strArray As String() = parameterPropertyName.Split(New Char() { "."c })
            Dim type As Type = Nothing
            Dim index As Integer = 0
            If (strArray.Length = 1) Then
                Dim parameterType As Type = Nothing
                If TypeOf parametersCollection Is CodeParameterDeclarationExpressionCollection Then
                    Dim expression As CodeParameterDeclarationExpression
                    For Each expression In DirectCast(parametersCollection, CodeParameterDeclarationExpressionCollection)
                        If (String.Compare("e", expression.Name, StringComparison.Ordinal) = 0) Then
                            parameterType = [TryCast](expression.UserData.Item(GetType(Type)),Type)
                        End If
                    Next
                ElseIf TypeOf parametersCollection Is ParameterInfo() Then
                    Dim info As ParameterInfo
                    For Each info In DirectCast(parametersCollection, ParameterInfo())
                        If (String.Compare("e", info.Name, StringComparison.Ordinal) = 0) Then
                            parameterType = info.ParameterType
                        End If
                    Next
                End If
                If (Not parameterType Is Nothing) Then
                    Dim str As String = strArray(0)
                    strArray = New String() { "e", str }
                End If
            End If
            If TypeOf parametersCollection Is CodeParameterDeclarationExpressionCollection Then
                Dim expression2 As CodeParameterDeclarationExpression
                For Each expression2 In DirectCast(parametersCollection, CodeParameterDeclarationExpressionCollection)
                    If (String.Compare(strArray(0), expression2.Name, StringComparison.Ordinal) = 0) Then
                        type = [TryCast](expression2.UserData.Item(GetType(Type)),Type)
                    End If
                Next
            Else
                If Not TypeOf parametersCollection Is ParameterInfo() Then
                    Return Nothing
                End If
                Dim info2 As ParameterInfo
                For Each info2 In DirectCast(parametersCollection, ParameterInfo())
                    If (String.Compare(strArray(0), info2.Name, StringComparison.Ordinal) = 0) Then
                        type = info2.ParameterType
                    End If
                Next
            End If
            If (strArray.Length = 1) Then
                Return type
            End If
            index = 1
            Do While ((index < strArray.Length) AndAlso (Not type Is Nothing))
                Dim propertyType As Type = Nothing
                Dim info3 As PropertyInfo
                For Each info3 In type.GetProperties
                    propertyType = Nothing
                    If (String.Compare(info3.Name, strArray(index), StringComparison.Ordinal) = 0) Then
                        propertyType = info3.PropertyType
                        Exit For
                    End If
                Next
                If (Not propertyType Is Nothing) Then
                    type = propertyType
                Else
                    Dim info4 As FieldInfo
                    For Each info4 In type.GetFields
                        propertyType = Nothing
                        If (String.Compare(info4.Name, strArray(index), StringComparison.Ordinal) = 0) Then
                            propertyType = info4.FieldType
                            Exit For
                        End If
                    Next
                    If (Not propertyType Is Nothing) Then
                        type = propertyType
                    ElseIf (propertyType Is Nothing) Then
                        Return Nothing
                    End If
                End If
                index += 1
            Loop
            If (index = strArray.Length) Then
                Return type
            End If
            Return Nothing
        End Function

        Private Shared Function GetTransactionalScopeParent(ByVal activity As Activity) As Activity
            Dim parent As Activity = activity
            Do While (Not parent Is Nothing)
                If (TypeOf parent Is CompensatableTransactionScopeActivity OrElse TypeOf parent Is TransactionScopeActivity) Then
                    Return parent
                End If
                parent = parent.Parent
            Loop
            Return parent
        End Function

        Private Shared Function IsFollowerInTxnlScope(ByVal parent As Activity, ByVal activity As Activity) As Boolean
            Dim activity2 As Activity = activity
            Do While (Not activity2 Is Nothing)
                If (activity2 Is parent) Then
                    Return True
                End If
                activity2 = activity2.Parent
            Loop
            Return False
        End Function

        Private Shared Function IsOwnerActivitySame(ByVal ownerActivityName As String, ByVal existingOwnerActivityName As String, ByVal currentActivity As Activity, ByVal existingActivity As Activity) As Boolean
            If ownerActivityName.Equals(existingOwnerActivityName) Then
                Return True
            End If
            Dim activityByName As Activity = currentActivity.GetActivityByName(ownerActivityName)
            If (activityByName Is Nothing) Then
                activityByName = Helpers.ParseActivityForBind(currentActivity, ownerActivityName)
            End If
            Dim activity2 As Activity = currentActivity.GetActivityByName(existingOwnerActivityName)
            If (activity2 Is Nothing) Then
                activity2 = Helpers.ParseActivityForBind(existingActivity, existingOwnerActivityName)
            End If
            Return (((Not activityByName Is Nothing) AndAlso (Not activity2 Is Nothing)) AndAlso activityByName.QualifiedName.Equals(activity2.QualifiedName))
        End Function

        Friend Shared Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As New ValidationErrorCollection
            Dim activity As Activity = [TryCast](obj,Activity)
            If (Not TypeOf activity Is CallExternalMethodActivity AndAlso Not TypeOf activity Is HandleExternalEventActivity) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(Activity).FullName }), "obj")
            End If
            Dim interfaceType As Type = If(TypeOf activity Is CallExternalMethodActivity, DirectCast(activity, CallExternalMethodActivity).InterfaceType, DirectCast(activity, HandleExternalEventActivity).InterfaceType)
            If (Not interfaceType Is Nothing) Then
                If interfaceType.ContainsGenericParameters Then
                    Dim item As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_GenericMethodsNotSupported"), New Object() { interfaceType.FullName }), &H155) { _
                        .PropertyName = "InterfaceType" _
                    }
                    errors.Add(item)
                    Return errors
                End If
                Dim attributes As Object() = interfaceType.GetCustomAttributes(GetType(ExternalDataExchangeAttribute), False)
                If (attributes.Length = 0) Then
                    Dim error2 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_ExternalDataExchangeException"), New Object() { interfaceType.FullName }), &H113) { _
                        .PropertyName = "InterfaceType" _
                    }
                    errors.Add(error2)
                    Return errors
                End If
                If (activity.Site Is Nothing) Then
                    Dim errors2 As ValidationErrorCollection = CorrelationSetsValidator.ValidateHostInterface(manager, interfaceType, activity)
                    If (errors2.Count <> 0) Then
                        errors.AddRange(errors2)
                        Return errors
                    End If
                End If
                Dim info As MemberInfo = Nothing
                If TypeOf activity Is CallExternalMethodActivity Then
                    If ((DirectCast(activity, CallExternalMethodActivity).MethodName Is Nothing) OrElse (DirectCast(activity, CallExternalMethodActivity).MethodName.Length = 0)) Then
                        Return errors
                    End If
                    Dim info2 As MethodInfo = interfaceType.GetMethod(DirectCast(activity, CallExternalMethodActivity).MethodName, (BindingFlags.Public Or BindingFlags.Instance))
                    If ((info2 Is Nothing) OrElse info2.IsSpecialName) Then
                        errors.Add(New ValidationError(SR.GetString("Error_MissingMethodName", New Object() { activity.Name, DirectCast(activity, CallExternalMethodActivity).MethodName }), &H528))
                        Return errors
                    End If
                    If info2.ContainsGenericParameters Then
                        Dim error3 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_GenericMethodsNotSupported"), New Object() { info2.Name }), &H155) { _
                            .PropertyName = "MethodName" _
                        }
                        errors.Add(error3)
                        Return errors
                    End If
                    info = info2
                Else
                    If ((DirectCast(activity, HandleExternalEventActivity).EventName Is Nothing) OrElse (DirectCast(activity, HandleExternalEventActivity).EventName.Length = 0)) Then
                        Return errors
                    End If
                    Dim info3 As EventInfo = interfaceType.GetEvent(DirectCast(activity, HandleExternalEventActivity).EventName, (BindingFlags.Public Or BindingFlags.Instance))
                    If (info3 Is Nothing) Then
                        errors.Add(New ValidationError(SR.GetString("Error_MissingEventName", New Object() { activity.Name, DirectCast(activity, HandleExternalEventActivity).EventName }), &H528))
                        Return errors
                    End If
                    info = info3
                End If
                attributes = interfaceType.GetCustomAttributes(GetType(CorrelationProviderAttribute), False)
                If (attributes.Length = 0) Then
                    Dim class4 As <>c__DisplayClass2
                    Dim correlator As CorrelationToken = [TryCast](activity.GetValue(If(TypeOf activity Is CallExternalMethodActivity, CallExternalMethodActivity.CorrelationTokenProperty, HandleExternalEventActivity.CorrelationTokenProperty)),CorrelationToken)
                    If (interfaceType.GetCustomAttributes(GetType(CorrelationParameterAttribute), False).Length = 0) Then
                        If (Not correlator Is Nothing) Then
                            errors.Add(New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_CorrelationTokenSpecifiedForUncorrelatedInterface"), New Object() { activity.QualifiedName, interfaceType }), &H119, False, "CorrelationToken"))
                        End If
                        Return errors
                    End If
                    If (activity.Parent Is Nothing) Then
                        Return errors
                    End If
                    If ((correlator Is Nothing) OrElse String.IsNullOrEmpty(correlator.Name)) Then
                        errors.Add(New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_MissingCorrelationTokenProperty"), New Object() { activity.QualifiedName }), &H109, False, "CorrelationToken"))
                        Return errors
                    End If
                    If String.IsNullOrEmpty(correlator.OwnerActivityName) Then
                        errors.Add(New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_MissingCorrelationTokenOwnerNameProperty"), New Object() { activity.QualifiedName }), &H109, False, "CorrelationToken"))
                        Return errors
                    End If
                    Dim qualifiedCorrelationToken As String = Nothing
                    Dim sourceActivity As Activity = activity.GetActivityByName(correlator.OwnerActivityName)
                    If (sourceActivity Is Nothing) Then
                        sourceActivity = Helpers.ParseActivityForBind(activity, correlator.OwnerActivityName)
                    End If
                    If (Not sourceActivity Is Nothing) Then
                        qualifiedCorrelationToken = sourceActivity.QualifiedName
                    End If
                    Dim seedActivity As Activity = Nothing
                    Dim parent As CompositeActivity = activity.Parent
                    Dim activity3 As Activity = parent
                    Dim flag As Boolean = False
                    Do While (Not parent Is Nothing)
                        If (TypeOf parent Is ReplicatorActivity AndAlso (seedActivity Is Nothing)) Then
                            seedActivity = parent
                        End If
                        If (qualifiedCorrelationToken = parent.QualifiedName) Then
                            flag = True
                        End If
                        activity3 = parent
                        parent = parent.Parent
                    Loop
                    If Not flag Then
                        Dim error4 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_OwnerActivityIsNotParent"), New Object() { activity.QualifiedName }), &H109) { _
                            .PropertyName = "CorrelationToken" _
                        }
                        errors.Add(error4)
                    End If
                    Dim flag2 As Boolean = False
                    attributes = info.GetCustomAttributes(GetType(CorrelationInitializerAttribute), False)
                    If (attributes.Length > 0) Then
                        flag2 = True
                    End If
                    If ((flag2 AndAlso (Not seedActivity Is Nothing)) AndAlso TypeOf activity Is HandleExternalEventActivity) Then
                        Dim error5 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_InitializerInReplicator"), New Object() { seedActivity.QualifiedName }), &H62D, False) { _
                            .PropertyName = "CorrelationToken" _
                        }
                        errors.Add(error5)
                    End If
                    If String.IsNullOrEmpty(qualifiedCorrelationToken) Then
                        Return errors
                    End If
                    If (Not seedActivity Is Nothing) Then
                        Dim CS$<>8__locals3 As <>c__DisplayClass2 = class4
                        Dim isValid As Boolean = False
                        Dim walker As New Walker
                        AddHandler walker.FoundActivity, Function (ByVal w As Walker, ByVal args As WalkerEventArgs) 
                            If (args.CurrentActivity.Enabled AndAlso (args.CurrentActivity.QualifiedName = CS$<>8__locals3.qualifiedCorrelationToken)) Then
                                isValid = True
                                args.Action = WalkerAction.Abort
                            End If
                        End Function
                        walker.Walk(seedActivity)
                        If Not isValid Then
                            Dim error6 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_CorrelationTokenInReplicator", New Object() { correlator.Name, seedActivity.QualifiedName }), New Object(0  - 1) {}), &H618, True) { _
                                .PropertyName = "CorrelationToken" _
                            }
                            errors.Add(error6)
                        End If
                    End If
                    If flag2 Then
                        Return errors
                    End If
                    Dim CS$<>8__locals3 As <>c__DisplayClass2 = class4
                    Dim isValid As Boolean = False
                    Dim ownerNameValidated As Boolean = False
                    Dim initFollowerInTxnlScope As Boolean = False
                    Dim walker2 As New Walker
                    AddHandler walker2.FoundActivity, Function (ByVal w As Walker, ByVal args As WalkerEventArgs) 
                        Dim currentActivity As Activity = args.CurrentActivity
                        If (currentActivity.Enabled AndAlso (TypeOf currentActivity Is CallExternalMethodActivity OrElse TypeOf currentActivity Is HandleExternalEventActivity)) Then
                            Dim token As CorrelationToken = [TryCast](currentActivity.GetValue(If(TypeOf currentActivity Is CallExternalMethodActivity, CallExternalMethodActivity.CorrelationTokenProperty, HandleExternalEventActivity.CorrelationTokenProperty)),CorrelationToken)
                            If (((Not token Is Nothing) AndAlso (Not TypeOf currentActivity Is CallExternalMethodActivity OrElse CS$<>8__locals3.interfaceType.Equals(DirectCast(currentActivity, CallExternalMethodActivity).InterfaceType))) AndAlso (Not TypeOf currentActivity Is HandleExternalEventActivity OrElse CS$<>8__locals3.interfaceType.Equals(DirectCast(currentActivity, HandleExternalEventActivity).InterfaceType))) Then
                                Dim info As MemberInfo = Nothing
                                If TypeOf currentActivity Is CallExternalMethodActivity Then
                                    If ((DirectCast(currentActivity, CallExternalMethodActivity).MethodName Is Nothing) OrElse (DirectCast(currentActivity, CallExternalMethodActivity).MethodName.Length = 0)) Then
                                        Return
                                    End If
                                    Dim method As MethodInfo = CS$<>8__locals3.interfaceType.GetMethod(DirectCast(currentActivity, CallExternalMethodActivity).MethodName, (BindingFlags.Public Or BindingFlags.Instance))
                                    If ((method Is Nothing) OrElse method.IsSpecialName) Then
                                        Return
                                    End If
                                    info = method
                                Else
                                    If ((DirectCast(currentActivity, HandleExternalEventActivity).EventName Is Nothing) OrElse (DirectCast(currentActivity, HandleExternalEventActivity).EventName.Length = 0)) Then
                                        Return
                                    End If
                                    Dim info3 As EventInfo = CS$<>8__locals3.interfaceType.GetEvent(DirectCast(currentActivity, HandleExternalEventActivity).EventName, (BindingFlags.Public Or BindingFlags.Instance))
                                    If (info3 Is Nothing) Then
                                        Return
                                    End If
                                    info = info3
                                End If
                                CS$<>8__locals3.attributes = info.GetCustomAttributes(GetType(CorrelationInitializerAttribute), False)
                                If (CS$<>8__locals3.attributes.Length <> 0) Then
                                    If TypeOf CS$<>8__locals3.activity Is HandleExternalEventActivity Then
                                        Dim transactionalScopeParent As Activity = CorrelationSetsValidator.GetTransactionalScopeParent(currentActivity)
                                        If ((Not transactionalScopeParent Is Nothing) AndAlso CorrelationSetsValidator.IsFollowerInTxnlScope(transactionalScopeParent, CS$<>8__locals3.activity)) Then
                                            initFollowerInTxnlScope = True
                                        End If
                                    End If
                                    CS$<>8__locals3.sourceActivity = CS$<>8__locals3.activity.GetActivityByName(token.OwnerActivityName)
                                    If (CS$<>8__locals3.sourceActivity Is Nothing) Then
                                        CS$<>8__locals3.sourceActivity = Helpers.ParseActivityForBind(CS$<>8__locals3.activity, token.OwnerActivityName)
                                    End If
                                    If (Not CS$<>8__locals3.sourceActivity Is Nothing) Then
                                        Dim qualifiedName As String = CS$<>8__locals3.sourceActivity.QualifiedName
                                    End If
                                    If ((CS$<>8__locals3.correlator.Name = token.Name) AndAlso CorrelationSetsValidator.IsOwnerActivitySame(CS$<>8__locals3.correlator.OwnerActivityName, token.OwnerActivityName, CS$<>8__locals3.activity, currentActivity)) Then
                                        isValid = True
                                        ownerNameValidated = True
                                        args.Action = WalkerAction.Abort
                                    End If
                                End If
                            End If
                        End If
                    End Function
                    walker2.Walk(activity3)
                    If Not isValid Then
                        Dim error7 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_UninitializedCorrelation"), New Object(0  - 1) {}), &H538, True) { _
                            .PropertyName = "CorrelationToken" _
                        }
                        errors.Add(error7)
                        If ownerNameValidated Then
                            error7 = New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_MisMatchCorrelationTokenOwnerNameProperty"), New Object() { correlator.Name }), &H538, False) { _
                                .PropertyName = "CorrelationToken" _
                            }
                            errors.Add(error7)
                        End If
                    End If
                    If initFollowerInTxnlScope Then
                        Dim error8 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_InitializerFollowerInTxnlScope"), New Object(0  - 1) {}), &H62E, False) { _
                            .PropertyName = "CorrelationToken" _
                        }
                        errors.Add(error8)
                    End If
                End If
            End If
            Return errors
        End Function

        Private Shared Function ValidateHostInterface(ByVal serviceProvider As IServiceProvider, ByVal interfaceType As Type, ByVal activity As Activity) As ValidationErrorCollection
            Dim service As Dictionary([Of] Type, ValidationErrorCollection) = [TryCast](serviceProvider.GetService(GetType(Dictionary([Of] Type, ValidationErrorCollection))),Dictionary([Of] Type, ValidationErrorCollection))
            If (service Is Nothing) Then
                service = New Dictionary([Of] Type, ValidationErrorCollection)
                Dim container As IServiceContainer = [TryCast](serviceProvider.GetService(GetType(IServiceContainer)),IServiceContainer)
                If (Not container Is Nothing) Then
                    container.AddService(GetType(Dictionary([Of] Type, ValidationErrorCollection)), service)
                End If
            End If
            If service.ContainsKey(interfaceType) Then
                Return New ValidationErrorCollection
            End If
            service.Add(interfaceType, New ValidationErrorCollection)
            If (interfaceType.GetCustomAttributes(GetType(CorrelationProviderAttribute), False).Length = 0) Then
                Dim customAttributes As Object() = interfaceType.GetCustomAttributes(GetType(ExternalDataExchangeAttribute), False)
                Dim objArray3 As Object() = interfaceType.GetCustomAttributes(GetType(CorrelationParameterAttribute), False)
                If ((customAttributes.Length <> 0) AndAlso (objArray3.Length <> 0)) Then
                    service.Item(interfaceType).AddRange(CorrelationSetsValidator.ValidateHostInterfaceMembers(interfaceType, activity))
                    service.Item(interfaceType).AddRange(CorrelationSetsValidator.ValidateHostInterfaceAttributes(interfaceType))
                Else
                    service.Item(interfaceType).AddRange(CorrelationSetsValidator.ValidateInvalidHostInterfaceAttributes(interfaceType))
                End If
            End If
            Return service.Item(interfaceType)
        End Function

        Private Shared Function ValidateHostInterfaceAttributes(ByVal interfaceType As Type) As ValidationErrorCollection
            If (interfaceType Is Nothing) Then
                Throw New ArgumentNullException("interfaceType")
            End If
            Dim validationErrors As New ValidationErrorCollection
            Dim list As New ArrayList
            Dim obj2 As Object
            For Each obj2 In interfaceType.GetCustomAttributes(GetType(CorrelationParameterAttribute), False)
                Dim attributeFromObject As CorrelationParameterAttribute = Helpers.GetAttributeFromObject([Of] CorrelationParameterAttribute)(obj2)
                If (String.IsNullOrEmpty(attributeFromObject.Name) OrElse (attributeFromObject.Name.Trim.Length = 0)) Then
                    Dim item As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationAttributeInvalid", New Object() { GetType(CorrelationParameterAttribute).Name, "Name", interfaceType.Name }), &H150)
                    item.UserData.Add(GetType(CorrelationParameterAttribute), interfaceType.Name)
                    validationErrors.Add(item)
                ElseIf list.Contains(attributeFromObject.Name) Then
                    Dim error2 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_DuplicateCorrelationAttribute", New Object() { GetType(CorrelationParameterAttribute).Name, attributeFromObject.Name, interfaceType.Name }), &H151)
                    error2.UserData.Add(GetType(CorrelationParameterAttribute), interfaceType.Name)
                    validationErrors.Add(error2)
                Else
                    list.Add(attributeFromObject.Name)
                End If
            Next
            Dim hashtable As New Hashtable
            Dim hashtable2 As New Hashtable
            Dim hashtable3 As New Hashtable
            Dim num As Integer = 0
            Dim info As MemberInfo
            For Each info In interfaceType.GetMembers
                If (TypeOf info Is MethodInfo AndAlso Not DirectCast(info, MethodInfo).IsSpecialName) Then
                    Dim hashtable4 As New Hashtable
                    hashtable2.Add(info, hashtable4)
                    CorrelationSetsValidator.FillCorrelationAliasAttrs(info, hashtable4, validationErrors)
                    Dim length As Integer = info.GetCustomAttributes(GetType(CorrelationInitializerAttribute), False).Length
                    num = (num + length)
                    If (length > 0) Then
                        Dim str As String
                        For Each str In list
                            Dim paramPath As String = str
                            If hashtable4.Contains(str) Then
                                paramPath = DirectCast(hashtable4.Item(str), CorrelationAliasAttribute).Path
                            End If
                            Dim type As Type = CorrelationSetsValidator.FetchParameterType(info, paramPath)
                            If ((Not type Is Nothing) AndAlso Not hashtable.ContainsKey(str)) Then
                                hashtable.Item(str) = type
                            End If
                        Next
                    End If
                ElseIf TypeOf info Is EventInfo Then
                    Dim num3 As Integer = info.GetCustomAttributes(GetType(CorrelationInitializerAttribute), False).Length
                    num = (num + num3)
                    Dim hashtable5 As New Hashtable
                    hashtable2.Add(info, hashtable5)
                    CorrelationSetsValidator.FillCorrelationAliasAttrs(info, hashtable5, validationErrors)
                    Dim delegateFromEvent As Type = Helpers.GetDelegateFromEvent(DirectCast(info, EventInfo))
                    delegateFromEvent.GetMethod("Invoke")
                    CorrelationSetsValidator.FillCorrelationAliasAttrs(delegateFromEvent, hashtable5, validationErrors)
                    Dim correlationAliasAttrs As New Hashtable
                    CorrelationSetsValidator.FillCorrelationAliasAttrs(delegateFromEvent, correlationAliasAttrs, validationErrors)
                    If (hashtable3.Item(delegateFromEvent) Is Nothing) Then
                        hashtable3.Add(delegateFromEvent, correlationAliasAttrs)
                    End If
                    If (num3 > 0) Then
                        Dim str3 As String
                        For Each str3 In list
                            Dim path As String = str3
                            If hashtable5.Contains(str3) Then
                                path = DirectCast(hashtable5.Item(str3), CorrelationAliasAttribute).Path
                            End If
                            Dim type3 As Type = CorrelationSetsValidator.FetchParameterType(info, path)
                            If ((Not type3 Is Nothing) AndAlso Not hashtable.ContainsKey(str3)) Then
                                hashtable.Item(str3) = type3
                            End If
                        Next
                    End If
                End If
            Next
            Dim entry As DictionaryEntry
            For Each entry In hashtable2
                Dim key As MemberInfo = [TryCast](entry.Key,MemberInfo)
                Dim hashtable7 As Hashtable = DirectCast(entry.Value, Hashtable)
                Dim str5 As String
                For Each str5 In hashtable7.Keys
                    If (Not list.Contains(str5) AndAlso (Not TypeOf key Is EventInfo OrElse (DirectCast(hashtable3.Item(Helpers.GetDelegateFromEvent(DirectCast(key, EventInfo))), Hashtable).Item(str5) Is Nothing))) Then
                        Dim error3 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationParameterNotFound", New Object() { GetType(CorrelationAliasAttribute).Name, str5, key.Name, GetType(CorrelationParameterAttribute).Name, interfaceType.Name }), &H153)
                        error3.UserData.Add(GetType(CorrelationAliasAttribute), key.Name)
                        validationErrors.Add(error3)
                    End If
                Next
            Next
            Dim str6 As String
            For Each str6 In list
                Dim entry2 As DictionaryEntry
                For Each entry2 In hashtable2
                    Dim str7 As String = str6
                    Dim info3 As MemberInfo = DirectCast(entry2.Key, MemberInfo)
                    Dim hashtable8 As Hashtable = DirectCast(entry2.Value, Hashtable)
                    If hashtable8.Contains(str6) Then
                        str7 = DirectCast(hashtable8.Item(str6), CorrelationAliasAttribute).Path
                    End If
                    Dim type4 As Type = CorrelationSetsValidator.FetchParameterType(DirectCast(entry2.Key, MemberInfo), str7)
                    If (type4 Is Nothing) Then
                        If (Not TypeOf info3 Is EventInfo OrElse (DirectCast(hashtable3.Item(Helpers.GetDelegateFromEvent(DirectCast(info3, EventInfo))), Hashtable).Item(str6) Is Nothing)) Then
                            Dim error4 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationInvalid", New Object() { If((info3.DeclaringType Is interfaceType), info3.Name, info3.DeclaringType.Name), str6 }), &H158)
                            error4.UserData.Add(GetType(CorrelationParameterAttribute), If((info3.DeclaringType Is interfaceType), info3.Name, info3.DeclaringType.Name))
                            validationErrors.Add(error4)
                        End If
                    ElseIf (hashtable.ContainsKey(str6) AndAlso (Not hashtable.Item(str6) Is type4)) Then
                        Dim error5 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationTypeNotConsistent", New Object() { str7, GetType(CorrelationAliasAttribute).Name, If((info3.DeclaringType Is interfaceType), info3.Name, info3.DeclaringType.Name), type4.Name, DirectCast(hashtable.Item(str6), Type).Name, str6, interfaceType.Name }), 340)
                        error5.UserData.Add(GetType(CorrelationAliasAttribute), If((info3.DeclaringType Is interfaceType), info3.Name, info3.DeclaringType.Name))
                        validationErrors.Add(error5)
                    End If
                Next
            Next
            If (num = 0) Then
                Dim error6 As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_CorrelationInitializerNotDefinied", New Object() { interfaceType.Name }), &H159)
                error6.UserData.Add(GetType(CorrelationInitializerAttribute), interfaceType.Name)
                validationErrors.Add(error6)
            End If
            Return validationErrors
        End Function

        Private Shared Function ValidateHostInterfaceMembers(ByVal interfaceType As Type, ByVal activity As Activity) As ValidationErrorCollection
            Dim errors As New ValidationErrorCollection
            Dim info As MemberInfo
            For Each info In interfaceType.GetMembers
                If ((TypeOf info Is MethodInfo OrElse TypeOf info Is EventInfo) AndAlso (Not TypeOf info Is MethodInfo OrElse Not DirectCast(info, MethodInfo).IsSpecialName)) Then
                    Dim method As MethodInfo = Nothing
                    Dim eventHandlerType As Type = Nothing
                    If TypeOf info Is EventInfo Then
                        Dim eventInfo As EventInfo = DirectCast(info, EventInfo)
                        eventHandlerType = eventInfo.EventHandlerType
                        If (eventHandlerType Is Nothing) Then
                            eventHandlerType = TypeProvider.GetEventHandlerType(eventInfo)
                        End If
                        If (eventHandlerType Is Nothing) Then
                            Throw New InvalidOperationException
                        End If
                        method = eventHandlerType.GetMethod("Invoke")
                    Else
                        method = DirectCast(info, MethodInfo)
                    End If
                    If method.IsGenericMethod Then
                        Dim item As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_GenericMethodsNotSupported"), New Object() { If(TypeOf info Is EventInfo, eventHandlerType.Name, method.Name) }), &H155)
                        If TypeOf info Is EventInfo Then
                            item.UserData.Add(GetType(EventInfo), DirectCast(info, EventInfo).Name)
                        Else
                            item.UserData.Add(GetType(MethodInfo), method.Name)
                        End If
                        errors.Add(item)
                    End If
                    If ((Not method.ReturnType Is GetType(Void)) AndAlso TypeOf info Is EventInfo) Then
                        Dim error2 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_ReturnTypeNotVoid"), New Object() { If(TypeOf info Is EventInfo, eventHandlerType.Name, method.Name) }), &H156)
                        If TypeOf info Is EventInfo Then
                            error2.UserData.Add(GetType(EventInfo), DirectCast(info, EventInfo).Name)
                        Else
                            error2.UserData.Add(GetType(MethodInfo), method.Name)
                        End If
                        errors.Add(error2)
                    End If
                    Dim info4 As ParameterInfo
                    For Each info4 In method.GetParameters
                        If (info4.IsOut OrElse info4.IsRetval) Then
                            Dim error3 As New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_OutRefParameterNotSupported"), New Object() { If(TypeOf info Is EventInfo, eventHandlerType.Name, method.Name), info4.Name }), &H157)
                            If TypeOf info Is EventInfo Then
                                error3.UserData.Add(GetType(EventInfo), DirectCast(info, EventInfo).Name)
                            Else
                                error3.UserData.Add(GetType(MethodInfo), method.Name)
                            End If
                            error3.UserData.Add(GetType(ParameterInfo), info4.Name)
                            errors.Add(error3)
                        End If
                    Next
                End If
            Next
            Return errors
        End Function

        Private Shared Function ValidateInvalidHostInterfaceAttributes(ByVal interfaceType As Type) As ValidationErrorCollection
            If (interfaceType Is Nothing) Then
                Throw New ArgumentNullException("interfaceType")
            End If
            Dim errors As New ValidationErrorCollection
            Dim flag As Boolean = False
            Dim info As MemberInfo
            For Each info In interfaceType.GetMembers
                If ((info.GetCustomAttributes(GetType(CorrelationInitializerAttribute), False).Length <> 0) OrElse (info.GetCustomAttributes(GetType(CorrelationAliasAttribute), False).Length <> 0)) Then
                    flag = True
                    Exit For
                End If
            Next
            If flag Then
                Dim item As New ValidationError(SR.GetString(CultureInfo.CurrentCulture, "Error_MissingCorrelationParameterAttribute", New Object() { interfaceType.Name }), &H152)
                item.UserData.Add(GetType(CorrelationParameterAttribute), interfaceType.Name)
                errors.Add(item)
            End If
            Return errors
        End Function

    End Class
End Namespace

